<?php

$folder = 'c:\excel';
$newfolder  = 'c:\excel2';

// $folder="E:\phpstudy\PHPTutorial\WWW\视频配图";
//
// $folder = "E:\phpstudy\PHPTutorial\WWW\新建文件夹";
// $newfolder  = 'E:\phpstudy\PHPTutorial\WWW\图片';
//


 //打开目录
$fp=opendir($folder);




 //阅读目录
while (false!=$file=readdir($fp)) {
    //列出所有文件并去掉'.'和'..'
    if ($file!='.' &&$file!='..') {
        //$file="$folder/$file";
        $file="$file";

        //赋值给数组
        $arr_file[]=$file;
    }
}
$str = '';


//var_dump($arr_file);

if (is_array($arr_file)) {
    foreach ($arr_file as $key => $value) {
        # code...
        $value=str_replace(".png", "", $value);//去掉后缀
        $code= getCode($value);
        $arr[$code]=$value;
    }
}

//遍历输出
foreach ($arr as $key => $value) {
    # code...
    $str.=makeSql($key, $value);
}
//var_dump($arr);
save('flag', $str);
exit("完成");
//exit($str);

//保存sql文件
function save($fileName, $string)
{  
    $newfolder  = 'c:\excel2';
    $fw = fopen($newfolder.'\\'.$fileName.".sql", "wr");
    var_dump($fw);
    fputs($fw, $string);
    fclose($fw);
}



//获取文件名的三字码
function getCode($name)
{
    # code...
    $name=str_replace("world-flag_", "", $name);
    $nameArr=explode("--", $name);
    $arr=explode("-", $nameArr[0]);
    return $arr[1];
}

//构造sql
function makeSql($name, $val)
{
    //$sql='UPDATE `country` set flag ="'.$val.'" WHERE country_code in (SELECT country_code FROM `country` WHERE country_code="'.$name.'")';
    $sql='UPDATE `country` set flag ="'.$val.'" WHERE country_code ="'.$name.'";'."\r\n";
    return $sql;
}

exit("测试");
//输出结果
 if (is_array($arr_file)) {
     while (list($key, $value)=each($arr_file)) {
         $name=strrev(substr(strrev($value), 4));

         $fw = fopen($newfolder.'\\'.$name.".png", "w");
         $fr = fopen($folder.'\\'.$value, "r");
         while (!feof($fr)) {
             //$line = fgets($fr);
                
             //fputs($fw, $line);
         }
         fclose($fr);
         fclose($fw);
     }
 }
